package pub.tbc.toolkit.config;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.Objs;

import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author tbc on 2016/9/8 11:18:23.
 */
@Slf4j
public class Config {

    public Set<Class<?>> getClassSet(String packageName) {
        Set<Class<?>> classSet = Sets.newHashSet();
        try {
            Enumeration<URL> urls = getClass().getClassLoader().getResources("");
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                if (url != null) {
                    String protocal = url.getProtocol();
                    switch (protocal) {
                        case "file":
                            String packagePath = url.getPath().replaceAll("%20", " ");
//                            addClass(classSet, packagePath, packageName);
                            break;
                        case "jar":
                            JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
                            if (Objs.nonNull(jarURLConnection)) {
                                JarFile jarFile = jarURLConnection.getJarFile();
                                if (Objs.nonNull(jarFile)) {
                                    Enumeration<JarEntry> jarEntries = jarFile.entries();
                                    while (jarEntries.hasMoreElements()) {
                                        JarEntry jarEntry = jarEntries.nextElement();
                                        String jarEntryName = jarEntry.getName();
                                        if (jarEntryName.endsWith(".properties")) {

                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
            }
        } catch (Exception e) {
            log.error("get class set failure ", e);
            throw new RuntimeException(e);
        }
        return classSet;
    }
}
